import {getAppointFreeStructure, getEnergySource, WORKSTATE, workSwitch} from "./public/CreepPublicAction.js";

/**
 * 采集者工作逻辑
 * @param {Creep} creep
 * @param sourcesIndex
 */
export const linkHarvesterRun = (creep, sourcesIndex) => {
    //工作逻辑转换
    workSwitch(creep, RESOURCE_ENERGY)
    if (creep.memory[WORKSTATE]) {
        workCore(creep)
    } else {
        getSources(creep, sourcesIndex)
    }

};

/**
 * 核心工作逻辑
 * @param {Creep} creep
 */
const workCore = (creep) => {
    //临时
    // getSources(creep)
    const link = Memory.Rooms[creep.room.name].links.sourceLink.map((id) => Game.getObjectById(id))
    if (link.length > 0) {
        if (creep.transfer(link[0], RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
            creep.moveTo(link[0], {visualizePathStyle: {stroke: '#ffffff'}});
        }
    } else {
        // 房间内没有link时
        getSources(creep)
    }
}

/**
 * 资源获取逻辑
 * @param {Creep} creep
 * @param sourcesIndex
 */
const getSources = (creep, sourcesIndex) => {
    getEnergySource(creep, sourcesIndex, "#3d5204")
}